package tr.edu.ege.seagent.hobnobandroidapplication.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import tr.edu.ege.seagent.hobnobandroidapplication.Place;
import tr.edu.ege.seagent.hobnobandroidapplication.activity.NotificationListActivity;
import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;

/**
 * This class provide us to accessing to the http servlet and gets response's
 * content.
 * 
 * @author etmen
 * 
 */
public class RetrieveNotificationsTask extends
		AsyncTask<String, Void, ArrayList<String>> {

	/**
	 * Class tag for logging.
	 */
	private static final String TAG = "HttpAccessForNotificationList";

	public RetrieveNotificationsTask(NotificationListActivity parent) {
		super();
		this.parent = parent;
	}

	private NotificationListActivity parent;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#doInBackground(Params[])
	 * 
	 * This function provides us to accessing http servlet and getting http
	 * response apart from the main thread.
	 */
	@Override
	protected ArrayList<String> doInBackground(String... params) {
		Log.i(TAG, "doInBackground METHOD");
		// client declaration...
		Log.d(TAG, "Instance of HttpCliend is being created.");
		HttpClient httpclient = new DefaultHttpClient();
		// create HttpGet object for we use doGet() function...
		Log.d(TAG, "HTTP GET is being called with given SERVLET_URL:"
				+ NotificationListActivity.NOTIFICATION_SERVLET_URL);
		HttpGet get = new HttpGet(
				NotificationListActivity.NOTIFICATION_SERVLET_URL);

		ArrayList<String> notificationList = new ArrayList<String>();

		try {
			// getting http response...
			Log.d(TAG, "HttpResponse is being gotten.");
			HttpResponse response = httpclient.execute(get);
			// getting response's content...
			Log.d(TAG, "Notification list is being gotten.");
			
			
			
			HttpEntity entity = response.getEntity();
			Log.i(TAG, "Response content is being gotten.");
			InputStream is = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
			Place place = new Gson().fromJson(reader, Place.class);
			
//			notificationList = convertSteamToString(response);
			notificationList = place.getNotifications();
		} catch (Exception e) {
			Log.wtf(TAG, e);
		}
		Log.d(TAG, "Notification list is being returned.");
		return notificationList;
	}

	/**
	 * 
	 * This function provides us to convert response's content to the string
	 * array.
	 * 
	 * @param response
	 *            HttpResponse
	 * @return ArrayList which is one row includes response's content line.
	 * 
	 */
	private ArrayList<String> convertSteamToString(HttpResponse response)
			throws IOException, UnsupportedEncodingException {

		Log.i(TAG, "convertSteamToString METHOD");
		HttpEntity entity = response.getEntity();
		Log.i(TAG, "Response content is being gotten.");
		InputStream is = entity.getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
		ArrayList<String> list = new ArrayList<String>();

		String line = null;
		// traverses response's content line by line...
		while ((line = reader.readLine()) != null) {
			// adds line to the list...
			Log.i(TAG, "Response line is : " + line);
			list.add(line);
		}
		is.close();
		Log.d(TAG, "List is being returned.");
		return list;
	}

	@Override
	protected void onPostExecute(ArrayList<String> result) {
		super.onPostExecute(result);
		parent.processNotifications(result);
	}
}